package com.fanfanwang.ffw.app.ui.my;

import com.fanfanwang.ffw.app.AppManager;
import com.fanfanwang.ffw.app.R;
import com.fanfanwang.ffw.app.adapter.MyRingAdapter;
import com.fanfanwang.ffw.app.adapter.MyRingAdapter.ViewHolder;
import com.fanfanwang.ffw.app.base.BaseActivity;
import com.fanfanwang.ffw.app.tools.SharedPreferencesUtil;
import com.fanfanwang.ffw.app.widget.CommonTitleBar;

import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.Toast;

/**
 * 铃声设置
 * @author Administrator
 *
 */
public class MyRingActivity extends BaseActivity {

	private MyRingAdapter mAdapter;
	ListView listView;
	Button sureBtn;
//	SharedPreferences sp;
//	SharedPreferences.Editor spe;
	private CommonTitleBar commonTitleBar;
	private MyRingActivity mAct;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_myring);
		AppManager.getAppManager().addActivity(this);
		/*初始化SharedPreferences*/

		/*初始化listView*/
		mAct=this;
		commonTitleBar = (CommonTitleBar) findViewById(R.id.common_titlebar);
		listView = (ListView) findViewById(R.id.ring_lv);
		mAdapter = new MyRingAdapter(this, SharedPreferencesUtil.getInstance(mAct).getRemindbellpos());
		listView.setAdapter(mAdapter);
		listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
		listView.setOnItemClickListener(mOnItemClickListener);
		setListener();
		/*初始化返回按钮和保存按钮*/

	}

	/*listView的按钮点击事件*/
	private OnItemClickListener mOnItemClickListener = new OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {

			ViewHolder mHolder = new ViewHolder(parent);
			/*设置Imageview不可被点击*/
			mHolder.iv.setClickable(false);
			/*清空map*/
			mAdapter.map.clear();
			// mAdapter.map.put(position, 1);
			/*将所点击的位置记录在map中*/
			mAdapter.map.put(position, true);
			/*刷新数据*/
			mAdapter.notifyDataSetChanged();
			/*判断位置不为0则播放的条目为position-1*/
			if (position != 0) {
				try {

					RingtoneManager rm = new RingtoneManager(MyRingActivity.this);
					rm.setType(RingtoneManager.TYPE_NOTIFICATION);
					rm.getCursor();
					rm.getRingtone(position - 1).play();

				} catch (Exception e) {
					e.printStackTrace();
				}
			}
			/*position为0是跟随系统，先得到系统所使用的铃声，然后播放*/
			if (position == 0) {

				Uri uri = RingtoneManager.getActualDefaultRingtoneUri(
						MyRingActivity.this, RingtoneManager.TYPE_NOTIFICATION);
				RingtoneManager.getRingtone(MyRingActivity.this, uri).play();
			}

		}

	};


	@Override
	protected void initView() {
		// TODO Auto-generated method stub

	}

	@Override
	protected void initData() {
		// TODO Auto-generated method stub

	}

	@Override
	protected void setListener() {
		// TODO Auto-generated method stub
		commonTitleBar.setCenterText(getString(R.string.bell_list));
		commonTitleBar.setLeftView(getString(R.string.cancel), new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				/*if(HomeActivity.leftRightSlidingMenu != null){
					HomeActivity.leftRightSlidingMenu.showMenu(true);
				}*/
				finish();

			}
		});
		commonTitleBar.setRightView(getString(R.string.confirm), new OnClickListener() {

			@Override
			public void onClick(View v) {
				String ringname=mAdapter.ringList.get(listView.getCheckedItemPosition());
				SharedPreferencesUtil.getInstance(mAct).putRemindbellpos(listView.getCheckedItemPosition());
				SharedPreferencesUtil.getInstance(mAct).putRemindbell(ringname);
				MsgSetAct.iRemindSoundable.setRemindSound(ringname);
				finish();
				Toast.makeText(MyRingActivity.this, getString(R.string.tishi_sound_save_success), Toast.LENGTH_SHORT)
				.show();
			}
		});
	}

}
